﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Framework;
using Ubatic.UI.Code;
using Ubatic.Contracts.Services;
using Ubatic.ServiceFactory;
using Ubatic.Contracts.Contracts;

namespace Ubatic.UI.Account
{
    public partial class Login : BasePage
    {
        private IUserService UserService = Factory.GetUserService();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SiteMaster masterPage = (SiteMaster)this.Master;
                masterPage.MakeMenuVisible(false);                               
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void btnIngresar_Click(object sender, EventArgs e)
        {
            try
            {  
             // Hago el login o no
                long cuit = Convert.ToInt64(this.txtCuitA.Text + this.txtCuitB.Text + this.txtCuitC.Text);
                
                UsuarioDto usuario = UserService.AutorizarUsuario(cuit, this.txtPassword.Text);
                if (usuario != null)
                {
                    Navigation.NavigateTo("/App/SeleccionDeRol.aspx");
                }
                else
                {
                    this.masterPage.ShowMessage("El usuario no está autorizado", WebMessageType.Notification);
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkRecordarContrasena_Click(object sender, EventArgs e)
        {
            try
            {
                Navigation.NavigateTo("/Account/RegenerarContrasena.aspx");
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkRegistrarUsuario_Click(object sender, EventArgs e)
        {
            try
            {
                Navigation.NavigateTo("/Account/Registrarse.aspx");
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }       
    }
}
